package com.srq.Utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {
    //获取连接数据库，并且创建执行sql语句的语句对象
    public static SqlSession getSqlSession(){
        try {
            //1.连接数据库
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            //2.创建工厂对象
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            //3.创建执行sql语句的语句对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
            return sqlSession;
        } catch (IOException e) {
            e.printStackTrace();
        }
      return null;
    }
}
